package com.rainkaze.IntelliDrawNote.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.rainkaze.IntelliDrawNote.R;
import com.rainkaze.IntelliDrawNote.bean.Notepad;
import com.rainkaze.IntelliDrawNote.databinding.ActivityEditNoteBinding;
import com.rainkaze.IntelliDrawNote.db.MyDbHelper;
import com.rainkaze.IntelliDrawNote.utils.TimeUtils;

import java.io.Serializable;

public class NoteEditActivity extends AppCompatActivity {

    private ActivityEditNoteBinding binding;

    private MyDbHelper myDbHelper;

    private Notepad notepad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding=ActivityEditNoteBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Serializable serializable = getIntent().getSerializableExtra("notepad");
        if (serializable != null){
            binding.titleTv.setText("修改记录");
            notepad=(Notepad) serializable;
            binding.contentEt.setText(notepad.getContent());
            binding.titleEt.setText(notepad.getTitle());
        }else{
            binding.titleTv.setText("添加记录");
        }

        String autoContent = getIntent().getStringExtra("ai_note_content");
        if (autoContent != null) {
            binding.contentEt.setText(autoContent); // 设置内容
        }



        binding.backIv.setOnClickListener(v -> finish());
        binding.clearIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.titleEt.setText("");
                binding.contentEt.setText("");
            }
        });

        myDbHelper=new MyDbHelper(this);
        binding.saveIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title=binding.titleEt.getText().toString().trim();
                String content=binding.contentEt.getText().toString().trim();
                if(title.isEmpty()){
                    Toast.makeText(NoteEditActivity.this, "请输入标题", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(content.isEmpty()){
                    Toast.makeText(NoteEditActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 判断是新增还是修改
                if (notepad==null){
                    notepad=new Notepad();
                    notepad.setContent(content);
                    notepad.setTitle(title);
                    notepad.setTime(TimeUtils.getTime());
                    myDbHelper.insertNote(notepad);
                    Toast.makeText(NoteEditActivity.this, "新增成功", Toast.LENGTH_SHORT).show();
                }else{
                    notepad.setContent(content);
                    notepad.setTitle(title);
                    notepad.setTime(TimeUtils.getTime());
                    myDbHelper.updateNote(notepad);
                    Toast.makeText(NoteEditActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
                }

                setResult(RESULT_OK);
                finish();
            }
        });
    }
}